package com.fgs.common.update;

import android.Manifest;
import android.content.Context;

import androidx.fragment.app.FragmentActivity;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.OnLifecycleEvent;

import com.fgs.common.CommonActivity;
import com.fgs.common.entity.UpdateResultInfo;
import com.fgs.common.interf.OnDialogClickListener;
import com.fgs.common.manager.CommonSharedPreferenceManager;
import com.fgs.common.permission.OnPermissionCheckResult;
import com.fgs.common.permission.PermissionHelper;
import com.fgs.common.utils.LogUtil;
import com.fgs.common.utils.ToastUtil;

/**
 * @author fgs
 * @date 2021/5/28
 * @description
 **/
public abstract class BaseUpdateStrategy<T extends UpdateResultInfo> implements UpdateStrategy<T>, LifecycleObserver {

    private UpdateManager updateManager;
    private UpdateDialog updateDialog;

    //防止内存泄漏
    @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
    public void onDestroy() {
        if (updateManager != null) {
            updateManager.startToUpdate(null, null);
            updateManager = null;
        }
        if (updateDialog != null) {
            updateDialog.setOnDialogClickListener(null);
            updateDialog = null;
        }
        LogUtil.print("BaseUpdateStrategy onDestroy");
    }

    @Override
    public void showUpdateTip(Context context, UpdateResultInfo updateInfo, OnUpdateCallback onUpdateCallback) {

        if (context instanceof CommonActivity) {
            ((CommonActivity) context).getLifecycle().addObserver(this);
        }

        updateDialog = new UpdateDialog(context);
        if (!updateInfo.isForceUpdate) { // 非强制更新
            boolean isShowUpdateDialog = CommonSharedPreferenceManager.getInstance().getNeedUpdateDate();
            if (!isShowUpdateDialog) {
                if (onUpdateCallback != null) {
                    onUpdateCallback.withoutUpdate();
                }
                return;
            }
        }

        updateDialog.setContent(updateInfo.updateContent);
        updateDialog.setDownloadUrl(updateInfo.downloadUrl);
        updateDialog.setForce(updateInfo.isForceUpdate);
        updateDialog.show();
        updateDialog.setOnDialogClickListener(new OnDialogClickListener() {
            @Override
            public void onPositiveClick() {

                //检查存储权限
                new PermissionHelper().checkPermissions(context, Manifest.permission.WRITE_EXTERNAL_STORAGE, new OnPermissionCheckResult() {
                    @Override
                    public void grantedResult() {
                        updateManager = UpdateManager.getInstance(context);
                        updateManager.startToUpdate(updateInfo, new OnUpdateListener() {
                            @Override
                            public void onStartUpdate() {
                                if (updateInfo.updateType == UpdateStrategy.TYPE_AUTO) {
                                    ToastUtil.showShortToast("开始下载...");
                                } else {
                                    ToastUtil.showShortToast("后台下载中...");
                                }
                            }

                            @Override
                            public void onProgress(int progress) {
                                if (onUpdateCallback != null)
                                    onUpdateCallback.updating(progress + "%");
                            }

                            @Override
                            public void onApkDownloadFinish(String apkPath) {
                                if (onUpdateCallback != null)
                                    onUpdateCallback.updateFinish();
                            }

                            @Override
                            public void onUpdateFailed() {
                                if (onUpdateCallback != null)
                                    onUpdateCallback.updateFailed();
                            }

                            @Override
                            public void onUpdateCanceled() {

                            }

                            @Override
                            public void onUpdateException() {
                                if (onUpdateCallback != null)
                                    onUpdateCallback.updateFailed();
                            }
                        });
                    }

                    @Override
                    public void shouldShowRequestPermissionRationale() {

                    }

                    @Override
                    public void grantedForbidResult() {
                        if (onUpdateCallback != null)
                            onUpdateCallback.updateFailed();

                    }
                });


            }

            @Override
            public void onNegativeClick(boolean isForce) {
                updateDialog.dismiss();
                if (onUpdateCallback != null) {
                    if (updateInfo.isForceUpdate) {
                        onUpdateCallback.exitUpdate();
                    } else {
                        onUpdateCallback.withoutUpdate();
                    }
                }

            }
        });
    }

}
